package com.yingda.electronicscaleadvices.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPool {

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int KEEP_ALIVE_TIME = 5000;



    private static ExecutorService executorService;
    private static ScheduledExecutorService scheduledExecutorService;
    private static ScheduledExecutorService scheduledExecutorServiceTwo;



    public static void  singleExecute(Runnable runnable){
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(runnable);
    }


    //android  开启定时任务
    public static void scheduleAtFixedRate(Runnable runnable){
        scheduledExecutorService = Executors.newScheduledThreadPool(5);
        scheduledExecutorService.scheduleAtFixedRate(runnable,0,1,TimeUnit.SECONDS);
    }

    public static void scheduleAtFixedRateTwo(Runnable runnable){
        scheduledExecutorServiceTwo = Executors.newScheduledThreadPool(1);
        //当前是1秒
        scheduledExecutorServiceTwo.scheduleAtFixedRate(runnable,0,1,TimeUnit.SECONDS);
    }

    //多线程运行

    //关闭线程池
    public static void closeDoubleExecute(){

        if (scheduledExecutorService!=null){
            scheduledExecutorService.shutdownNow();
            scheduledExecutorService=null;
        }
    }

    public static void closeDoubleExecuteTwo(){

        if (scheduledExecutorServiceTwo!=null){
            scheduledExecutorServiceTwo.shutdownNow();
            scheduledExecutorServiceTwo=null;
        }
    }

    //关闭单个线程池
    public static void closeSingleExecute(){
        if (executorService!=null){
            executorService.shutdown();
            executorService = null;
        }

    }
}

